import { createRouter, createWebHashHistory } from 'vue-router'
import { useAppStoreWithOut } from '@/store/modules/app'
import Layout from '@/components/Layout/index.vue'
const routerList = [] //路由集合

const appStore = useAppStoreWithOut()

const constantRouterMap = [
  {
    path: '/login',
    name: 'Login',
    meta: {
      title: '登录',
    },
    component: () => import(/*webpackChunkName: "login"*/ '@/views/Login.vue'),
  },
  {
    path: '/register',
    name: 'Register',
    meta: {
      title: '注册',
    },
    component: () => import(/*webpackChunkName: "register"*/ '@/views/Register.vue'),
  },
  {
    path: '/forgetPassword',
    name: 'ForgetPassword',
    meta: {
      title: '找回密码',
    },
    component: () => import(/*webpackChunkName: "forgetPassword"*/ '@/views/ForgetPassword.vue'),
  },
  {
    path: '/404',
    name: '404',
    meta: {
      title: '404',
    },
    component: () => import(/*webpackChunkName: "404"*/ '@/views/404.vue'),
  },
  //   {
  //     path: '/:pathMatch(.*)',
  //     component: () => import('@/views/404.vue'),
  //   },
]
const adminRoutes = [
  {
    path: '/',
    redirect: '/admin/files',
  },
  {
    path: '/admin',
    name: 'Admin',
    redirect: '/admin/files',
    component: Layout,
    children: [
      {
        path: 'files',
        name: 'Files',
        meta: {
          title: '文件管理',
          icon: 'icon-jichuxinxiguanli',
        },
        component: () => import(/*webpackChunkName: "files"*/ '@/views/admin/files.vue'),
      },
      {
        path: 'users',
        name: 'Users',
        meta: {
          title: '用户管理',
          icon: 'icon-jiaoseguanli',
        },
        component: () => import(/*webpackChunkName: "users"*/ '@/views/admin/users.vue'),
      },
      {
        path: 'payment',
        name: 'Payment',
        meta: {
          title: '付款记录',
          icon: 'icon-chongzhi',
        },
        component: () => import(/*webpackChunkName: "payment"*/ '@/views/admin/payment.vue'),
      },
      {
        path: 'logs',
        name: 'Logs',
        meta: {
          title: '操作日志',
          icon: 'icon-rizhiguanli',
        },
        component: () => import(/*webpackChunkName: "logs"*/ '@/views/admin/logs.vue'),
      },
      {
        path: 'settings',
        name: 'Settings',
        meta: {
          title: '前台配置',
          icon: 'icon-zhanghaoguanli',
        },
        component: () => import(/*webpackChunkName: "settings"*/ '@/views/admin/settings.vue'),
      },
    ],
  },
  {
    path: '/:path(.*)*',
    redirect: '/404',
    name: '404Page',
  },
]
const webRoutes = [
  {
    path: '/',
    redirect: '/web/upload',
  },
  {
    path: '/web',
    name: 'Web',
    redirect: '/web/upload',
    component: Layout,
    children: [
      {
        path: 'upload',
        name: 'Upload',
        meta: {
          title: '上传文件',
        },
        component: () => import(/*webpackChunkName: "upload"*/ '@/views/web/upload.vue'),
      },
      {
        path: 'records',
        name: 'Records',
        meta: {
          title: '上传记录',
        },
        component: () => import(/*webpackChunkName: "records"*/ '@/views/web/records.vue'),
      },
    ],
  },
  {
    path: '/:path(.*)*',
    redirect: '/404',
    name: '404Page',
  },
]
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRouterMap,
  strict: true,
  scrollBehavior: () => ({ left: 0, top: 0 }),
})

const whiteList = ['Login', 'Register', 'ForgetPassword'] // 不重定向白名单

export const resetRouter = () => {
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !whiteList.includes(name)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

router.beforeEach(async (to, from, next) => {
  if (to.meta && to.meta.title) {
    document.title = to.meta.title
  }
  let userinfo = JSON.parse(localStorage.getItem('userinfo'))
  if (userinfo) {
    if (to.path === '/login') {
      localStorage.removeItem('userinfo')
      // 清除全局状态
      appStore.delState()
      // 重置静态路由
      resetRouter()
      next({ path: '/login' })
    } else {
      if (appStore.getRoutes) {
        next()
      } else {
        let permissionRoutes = userinfo.userType === 1 ? webRoutes : adminRoutes
        permissionRoutes.forEach((route) => {
          router.addRoute(route) // 动态添加可访问路由表
        })
        appStore.setRoutes(permissionRoutes)
        appStore.setUserinfo(userinfo)
        next({ ...to, replace: true })
      }
    }
  } else {
    if (whiteList.indexOf(to.name) !== -1) {
      next()
    } else {
      next(`/login`) // 否则全部重定向到登录页
    }
  }
})

export { routerList }

export default router
